﻿using System;
using System.Collections.Generic;
using System.Linq;
using Character;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x02000146 RID: 326
public class Hairs
{
	// Token: 0x060007E0 RID: 2016 RVA: 0x0002F068 File Offset: 0x0002D268
	public Hairs(Human human, Transform parent)
	{
		this.human = human;
		this.hirsParent = parent;
		this.isSetHair = false;
	}

	// Token: 0x170000E9 RID: 233
	// (get) Token: 0x060007E1 RID: 2017 RVA: 0x0002F094 File Offset: 0x0002D294
	public GameObject[] objHairs
	{
		get
		{
			return (from v in this.parts
			select (v == null) ? null : v.obj).ToArray<GameObject>();
		}
	}

	// Token: 0x170000EA RID: 234
	// (get) Token: 0x060007E2 RID: 2018 RVA: 0x0002F0C4 File Offset: 0x0002D2C4
	public DynamicBone[] dynamicBone
	{
		get
		{
			List<DynamicBone> list = new List<DynamicBone>();
			foreach (HairObj hairObj in from v in this.parts
			where v != null
			where !v.dynamicBone.IsNullOrEmpty<DynamicBone>()
			select v)
			{
				list.AddRange(hairObj.dynamicBone);
			}
			return list.ToArray();
		}
	}

	// Token: 0x170000EB RID: 235
	// (get) Token: 0x060007E3 RID: 2019 RVA: 0x0002F174 File Offset: 0x0002D374
	public Transform HirsParent
	{
		get
		{
			return this.hirsParent;
		}
	}

	// Token: 0x170000EC RID: 236
	// (get) Token: 0x060007E4 RID: 2020 RVA: 0x0002F17C File Offset: 0x0002D37C
	// (set) Token: 0x060007E5 RID: 2021 RVA: 0x0002F184 File Offset: 0x0002D384
	public bool isSetHair { get; private set; }

	// Token: 0x060007E6 RID: 2022 RVA: 0x0002F190 File Offset: 0x0002D390
	public void SetHairsParent(Transform parent)
	{
		this.hirsParent = parent;
	}

	// Token: 0x060007E7 RID: 2023 RVA: 0x0002F19C File Offset: 0x0002D39C
	public void Load(HairParameter param)
	{
		if (this.hirsParent == null)
		{
			GlobalData.Debug_LogError("没有头发的父母");
			return;
		}
		for (int i = 0; i < 3; i++)
		{
			if (this.parts[i] != null)
			{
				this.parts[i].obj.transform.SetParent(null);
				UnityEngine.Object.DestroyImmediate(this.parts[i].obj);
				this.parts[i] = null;
			}
		}
		if (this.human.sex == SEX.FEMALE)
		{
			HairData hair_Front = CustomDataManager.GetHair_Front(param.parts[1].ID);
			HairData hair_Side = CustomDataManager.GetHair_Side(param.parts[2].ID);
			BackHairData hair_Back = CustomDataManager.GetHair_Back(param.parts[0].ID);
			HairData[] array = new HairData[]
			{
				hair_Back,
				hair_Front,
				hair_Side
			};
			if (hair_Back != null && hair_Back.isSet)
			{
				param.parts[1].ID = 0;
				param.parts[2].ID = 0;
				this.isSetHair = true;
			}
			else
			{
				this.isSetHair = false;
			}
			for (int j = 0; j < 3; j++)
			{
				HairData hair_Female = CustomDataManager.GetHair_Female((HAIR_TYPE)j, param.parts[j].ID);
				if (hair_Female != null)
				{
					if (!(hair_Female.prefab == "-"))
					{
						GameObject obj = AssetBundleLoader.LoadAndInstantiate<GameObject>(hair_Female.assetbundleDir, hair_Female.assetbundleName, hair_Female.prefab);
						hair_Female.isNew = false;
						this.parts[j] = new HairObj(obj, this.hirsParent);
						this.SetLayer(j);
					}
				}
			}
		}
		else if (this.human.sex == SEX.MALE)
		{
			int num = 0;
			BackHairData hair_Male = CustomDataManager.GetHair_Male(param.parts[num].ID);
			GameObject obj2 = AssetBundleLoader.LoadAndInstantiate<GameObject>(hair_Male.assetbundleDir, hair_Male.assetbundleName, hair_Male.prefab);
			hair_Male.isNew = false;
			this.parts[num] = new HairObj(obj2, this.hirsParent);
			this.SetLayer(num);
			this.isSetHair = true;
		}
		this.ChangeColor(param);
		this.human.SetupDynamicBones();
		Resources.UnloadUnusedAssets();
	}

	// Token: 0x060007E8 RID: 2024 RVA: 0x0002F3DC File Offset: 0x0002D5DC
	public void ChangeColor(HairParameter param)
	{
		for (int i = 0; i < 3; i++)
		{
			if (this.parts[i] != null)
			{
				this.parts[i].ChangeColor(param.parts[i]);
			}
		}
	}

	// Token: 0x060007E9 RID: 2025 RVA: 0x0002F420 File Offset: 0x0002D620
	public void DetachHairs()
	{
		for (int i = 0; i < this.parts.Length; i++)
		{
			if (this.parts[i] != null)
			{
				this.parts[i].obj.transform.SetParent(null);
			}
		}
	}

	// Token: 0x060007EA RID: 2026 RVA: 0x0002F46C File Offset: 0x0002D66C
	public void AttachHairs(Transform parent)
	{
		this.hirsParent = parent;
		for (int i = 0; i < this.parts.Length; i++)
		{
			if (this.parts[i] != null)
			{
				this.parts[i].SetParent(this.hirsParent);
			}
		}
	}

	// Token: 0x060007EB RID: 2027 RVA: 0x0002F4BC File Offset: 0x0002D6BC
	public void ChangeShow(bool show)
	{
		for (int i = 0; i < this.parts.Length; i++)
		{
			if (this.parts[i] != null)
			{
				this.parts[i].obj.SetActive(show);
			}
		}
	}

	// Token: 0x060007EC RID: 2028 RVA: 0x0002F504 File Offset: 0x0002D704
	private void SetLayer(int partNo)
	{
		Renderer[] componentsInChildren = this.parts[partNo].obj.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer renderer in componentsInChildren)
		{
			renderer.gameObject.layer = this.hirsParent.gameObject.layer;
			renderer.lightProbeUsage = LightProbeUsage.Off;
		}
	}

	// Token: 0x060007ED RID: 2029 RVA: 0x0002F564 File Offset: 0x0002D764
	public bool HasAcce(HAIR_TYPE part)
	{
		return this.parts[(int)part] != null && this.parts[(int)part].hasAcce;
	}

	// Token: 0x060007EE RID: 2030 RVA: 0x0002F590 File Offset: 0x0002D790
	public GameObject GetObj(HAIR_TYPE _type)
	{
		return (this.parts[(int)_type] == null) ? null : this.parts[(int)_type].obj;
	}

	// Token: 0x040007B8 RID: 1976
	private Human human;

	// Token: 0x040007B9 RID: 1977
	private HairObj[] parts = new HairObj[3];

	// Token: 0x040007BA RID: 1978
	private Transform hirsParent;
}
